#!/usr/bin/env ruby
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
# Main program file
require 'includes/plagtion_logger'
require 'modules/plagtion_mod'
require 'includes/autoload'
    
class PlagtionGlade
	include GetText
	attr :glade
	
	def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
		bindtextdomain(domain, localedir, nil, "UTF-8")
		@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
		# The program will directly end upon 'delete_event'.
		@glade["mainWindow"].signal_connect('delete_event') { Gtk.main_quit }
		
		@glade["initButton"].signal_connect('clicked') do |w|	# click event
			
    @glade["masterDocumentEntry"].text = "/home/trampfox/Projects/ya-plagtion/doc/gpl.txt"  
      if controlInputs()
				@glade["showProgress"].show
				Gtkgui.process_gui_events
				on_initButton_clicked(w)
			end # if
      
		end #signal
		
		# sets the options
    Configurations.set_from_glade(@glade)
	
  end
		
	def on_browseMaster_clicked(widget)
		filename = Gtkgui.onBrowse(widget, 0)									# 0 = master document
		@glade["masterDocumentEntry"].text = filename
		@master = filename 														# set the master file
	end
	
	def on_about_activate(widget)
		# Set application data that will be displayed in the
		# main dialog.
		@glade["aboutPlagtion"].show
	end
	
	def on_browseCopy_clicked(widget)
		filename_copy = Gtkgui.onBrowse(widget, 1)								# 1 = copy document
		@glade["copyDocumentEntry"].text = filename_copy
		@copy = filename_copy													# set the copy file
	end
	
	def on_webButton_clicked(widget)
		@glade["browseCopy"].sensitive = false
	end
	
	def on_localButton_clicked(widget)
		@glade["browseCopy"].sensitive = true
	end
	
	# main quit button clicked
	def on_quitButton_clicked(widget)
		system("rm tmp/*")
		puts "Bye bye"
		Gtk.main_quit
	end
	
	# apply options button clicked (Options notebook)
	def on_applyOptionsButton_clicked(widget)
		
    # add search engine options
		Configurations.set_from_glade(@glade)
    
	end
	
	def on_initButton_clicked(widget)
		@glade["infoLabel1"].text = ""														# init the info labels
		@glade["infoLabel2"].text = ""
		@glade["infoLabel3"].text = ""
		progressWindow = @glade["showProgress"]
		progressbar = @glade["progressbar1"]
		mainWindow = @glade["mainWindow"]
		@glade["infoLabel3"].text = "Creating MasterDocument"
		Gtkgui.updateProgressBar(progressbar, "5 % Complete", 5.0 / 100.0)
		# check the type of detection (local/internet)
		if @glade["localButton"].active?
      Configurations.set_local(true)
		else
      Configurations.set_local(false)
		end # if
		overlaps = []                                       # List of Overlap object
		puts "== Create MasterDocument =="
		doc = MasterDocument.new(@master, @copy, @glade)    # input document
		if doc.num_words > 0															  # do this only if the document has been readed
			$logger.info("Plagtion") {"Master Document name: #{doc.doc_name}"}
			#puts "call search common region"
			doc.search_common_region(glade)	
			totol = doc.display_overlaps()										# totol contains the number of overlaps founds
			@overlaps = doc.get_overlaps()										# get the overlpas list
      if (@overlaps)                                    # call this only if @overlaps contains 1 or more objects
        Gtkgui.searchOperationComplete(@glade, @overlaps[0], totol)
      else
        Gtkgui.searchOperationComplete(@glade, nil, 0)
      end
		else
			Gtkgui.showWarning(@glade)
			progressWindow.hide
		end # if
		
	end

	# showProgress widget methods
	def on_showResultsButton_clicked(widget)
		# show the overlaps with a ListSotre widget
		# @master contains the path of the master document (string)
		# @overlaps contains the list with the overlaps found on the internet
		Gtkgui.showListStore(@glade, @master, @overlaps)
		@glade["showProgress"].hide
	end
	
	def on_cancelButton_clicked(widget)
		puts "Closing"
		@glade["showProgress"].hide
	end
	
	def on_closeButton_clicked(widget)
		@glade["showProgress"].hide
		@glade["mainWindow"].show
	end

	private
# Private method

	def controlInputs()
		Gtkgui.controlInputs(@glade["mainWindow"], @glade["localButton"], @glade["webButton"], @glade["masterDocumentEntry"], @glade["copyDocumentEntry"])
	end
	
end #class

	# Main program
	if __FILE__ == $0
		# Set values as your own application. 
		PROG_PATH = "plagtion_temp.glade"
		PROG_NAME = "Plagtion"
		PlagtionGlade.new(PROG_PATH, nil, PROG_NAME)
		Gtk.main
	end


# TODO: use Nokogiri instead Hpricot for html parsing
# TODO: Clean-up code
# TODO: inforamarsi per rdoc per creare la documentazione del programma
